<template>
  <div class="transferRecord zy-content">
    <!-- 模具与设备 -->
    <div class="search clear">
      <div class="search-box-layout">
        <Row :gutter="24">
          <Col span="4">
            <p class="label">调拨单号</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.productInCode"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">出库仓库</p>
            <div class="ipt">
              <Select v-model="searchParams.outBackupConfig"
                      filterable
                      clearable
                      :placeholder="`${$t('common.selectInfo')}`">
                <Option v-for="item of backupConfigList"
                        :value="item.backupName"
                        :key="item.backupName"
                        :label="item.backupName">
                </Option>
              </Select>
            </div>
          </Col>
          <Col span="4">
            <p class="label">入库仓库</p>
            <div class="ipt">
              <Select v-model="searchParams.enterBackupConfig"
                      filterable
                      clearable
                      :placeholder="`${$t('common.selectInfo')}`">
                <Option v-for="item of backupConfigList"
                        :value="item.backupName"
                        :key="item.backupName"
                        :label="item.backupName">
                </Option>
              </Select>
            </div>
          </Col>
          <Col span="4">
            <p class="label">物料编号</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.materielCode"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">物料名称</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.materielName"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">图号规格</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.customMaterialCode"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">材料牌号</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.materialMark"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">操作人</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.operator"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">标签流水号</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.tagSerialNumber"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">批次号</p>
            <div class="ipt">
              <Input type="text" clearable v-model="searchParams.batchNumber"/>
            </div>
          </Col>
          <Col span="4">
            <p class="label">操作时间</p>
            <div class="ipt">
              <DatePicker @on-change="operatorTimeChange" v-model="operatorTime"
                          format="yyyy-MM-dd" type="daterange"
                          placeholder="请选择日期"/>
            </div>
          </Col>
          <div>
            <Col :span="4">
              <div>
                <p class="label hide-label">--</p>
                <div class="ipt">
                  <Button type="primary" class="search-btn" @click="changeNum()">{{ $t("common.Query") }}</Button>
                  <Button class="reset-btn" @click="reset()">{{ $t("common.reset") }}</Button>
                </div>
              </div>
            </Col>
          </div>
        </Row>
      </div>
    </div>
    <div class="alarmConfiguration">
      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">{{ $t("common.listInformation") }}</span>
          <Button  @click="exportTemplateClick">{{$t('common.batchExport')}}</Button>
          <Button @click="revoke" v-has="'transferRecordCheXiao'">撤销</Button>
        </div>
        <!-- 表格 -->
        <div class="tableContent">
          <Table class="resizable-table"
                 border
                 :columns="columnsList"
                 @on-column-width-resize="onColumnWidthResize"
                 ellipsis
                 ref="selection"
                 stripe
                 :data="dataArr"
                 @on-selection-change="onSelectChange"></Table>
        </div>
        <!-- 分页 -->
        <div class="pageWrap mt16">
          <Page :total="total" :page-size="pageSize" :page-size-opts="pageSizeOption" :current="pageNum"
                @on-change="changePageNum" @on-page-size-change="changePageSize" show-sizer show-total/>
        </div>
      </div>
    </div>

    <inputModal ref="inputModal"></inputModal>
  </div>
</template>
<script>
  import { getList, getBackupConfigList } from "@/api/system/transferRecord.js";
  import inputModal from "@/components/modalTypes/inputModal.vue";
  import fileDownload from 'js-file-download';
  import toggleTableOperation from "@/mixins/toggleTableOperation";

  export default {
    name: "transferRecord", //现场仓调拨记录
    components: {inputModal},
    mixins: [toggleTableOperation],

    data() {
      return {
        operatorTime: '',
        searchParams: {},
        backupConfigList: [],
        //一级table配置
        columns: [
          {
            key: "selection",
            type: "selection",
            width: 60,
            align: "center",
          },
          {
            title: "序号",
            type: 'index',
            width: 80,
            align: 'center'
          },
          {
            title: "调拨单号",
            key: "productInCode",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "出库位置",
            key: "outWarehouseLocation",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "出库仓库",
            key: "outBackupConfig",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "物料编号",
            key: "materielCode",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "物料名称",
            key: "materielName",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "物料型号",
            key: "materielModel",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "标签流水号",
            key: "tagSerialNumber",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "数量",
            key: "outNumber",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "批次号",
            key: "batchNumber",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "入库位置",
            key: "enterWarehouseLocation",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "入库仓库",
            key: "enterBackupConfig",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "材料牌号",
            key: "materialMark",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "图号规格",
            key: "customMaterialCode",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "状态",
            key: "state",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "撤销人",
            key: "revocationOperator",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "撤销时间",
            key: "revocationTime",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "撤销原因",
            key: "revocationReason",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "操作人",
            key: "operator",
            width: 100,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "操作时间",
            key: "operatorTime",
            width: 200,
            align: "center",
            resizable: true,
            tooltip: true,
          },
          {
            title: "操作结果",
            key: "handleResult",
            width: 200,
            align: "center",
            resizable: true,
            tooltip: true,
          },
        ],
        dataArr: [],
        checkDataList: [],
        allCheck: false,
        pageNum: 1,
        pageSize: 10,
        total: 0,
        pageSizeOption: [10, 20, 30, 40],
      };
    },
    mounted() {
      this.getTableColumnsWidth(this.columns);
      this.getBackupConfigList();
      this.searchList();
    },
    methods: {
      //勾选列表
      onSelectChange(selection) {
        let myArray = [];
        selection.map((item) => {
          myArray.push(item);
        });
        this.checkDataList = myArray;

        if (selection.length > 0) {
          this.allCheck = true;
        } else {
          this.allCheck = false;
        }
      },
      revoke() {
        if (this.checkDataList.length === 0) {
          this.$Message.warning("请选择需要撤销的数据");
          return;
        }
        this.$refs.inputModal.showModal({
          title: "撤销",
          desc: "撤销原因",
          submit: (form) => {
            let data = {
              idList: []
            };
            for (let i = 0; i < this.checkDataList.length; i++) {
              data['idList'].push(this.checkDataList[i].id);
            }
            data['revocationReason'] = form.value;
            this.axios({
              method: "post",
              url: "/dms/warehouseAllocation/updateState",
              data,
            }).then((res) => {
              if (res.code === 200) {
                this.$Message.success(res.msg);
                this.searchList();
              } else {
                this.$Message.warning({
                  content: res.msg,
                  duration: 6,
                  closable: true,
                });
              }
            });
          },
        });
      },
      getBackupConfigList() {
        getBackupConfigList({
          pageNum: 1,
          pageSize: 9999,
        }).then((res) => {
          if (res.code === 200) {
            this.backupConfigList = res.data.records;
          } else {
            this.$Message.warning({
              content: res.msg,
              duration: 6,
              closable: true,
            });
          }
        });
      },
      operatorTimeChange(time) {
        this.searchParams.beginTime = time[0];
        this.searchParams.endTime = time[1];
      },
      reset() {
        this.searchParams = {};
        this.changeNum();
      },
      //分页
      changeNum() {
        this.pageNum = 1;
        this.searchList();
      },
      changePageSize(pageSize) {
        this.pageSize = pageSize;
        this.searchList();
      },
      changePageNum(val) {
        this.pageNum = val;
        this.searchList();
      },
      //查询列表
      searchList() {
        let params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        };
        for (let key in this.searchParams) {
          if (this.searchParams[key]) {
            this.$set(params, key, this.searchParams[key]);
          }
        }
        getList(params).then((res) => {
          if (res.code === 200) {
            this.dataArr = res.data.list;
            this.total = res.data.total;
            for (let i = 0; i < this.dataArr.length; i++) {
              if (this.dataArr[i].onSiteWarehouse !== '否') {
                this.dataArr[i]._disabled = true;
              }
            }
          } else {
            this.$Message.warning({
              content: res.msg,
              duration: 6,
              closable: true,
            });
          }
        });
      },
      exportTemplateClick() {
        let params = {
          ...this.searchParams
        };
        const filName = "现场仓调拨记录";
        this.axios({
          method: "post",
          url: "/dms/warehouseAllocation/export",
          responseType: "blob",
          data: params
        }).then(res => {
          fileDownload(res, filName + ".xlsx");
        });
      },
    },
  };
</script>
<style scoped lang="scss">
  .fieldManage {
    .formList {
      padding: 30px;
      .btn {
        margin-top: 20px;
        text-align: center;
        width: 100%;
        height: 40px;
      }
      .inputBorder {
        border: 1px solid #dcdee2;
        border-radius: 4px;
      }
    }
  }
</style>